﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using NHibernate.Collection;
using NHibernate.Engine;
using NHibernate.Type;
using NHibernate.Persister.Collection;

namespace hbse.Persistence
{
    [Serializable]
    public class BindingBagType<T> : BagType
    {
        public BindingBagType(string role, string propertyRef)
            : base(role, propertyRef, false)
        {
        }

        public override IPersistentCollection Instantiate(ISessionImplementor session, ICollectionPersister persister, object key)
        {
            return new PersistentBindingBag<T>(session);
        }

        public override Type ReturnedClass
        {
            get { return typeof(IList<T>); }
        }

        public override IPersistentCollection Wrap(ISessionImplementor session, object collection)
        {
            return new PersistentBindingBag<T>(session, (IList<T>)collection);
        }

        public override object Instantiate(int anticipatedSize)
        {
            return new BindingList<T> { AllowNew = true };
        }
    }
}
